<?php
/**
 * Nadëb (Makú-Nadëb)
 *
 * @filesource 
 * @copyright  Copyright 2010 mateusweb.com.br
 * @license    Freeware
 * @package    Nadeb_Report
 * @subpackage Nadeb.Report.Report
 * @version    1.0
 */

class Nadeb_Report_Sql
{
	/*
	 * Public Vars
	 */
	public $sql;
	public $data;
	public $title;
	public $key;
	public $table;
	public $size;
	public $page;
	public $totalPages;
	public $readOnly;
	public $params;
	public $columns;
	public $tools;
	
	public $headerConfigs;
	public $footerConfigs;
	public $bodyConfigs;
	
	/*
	 * Protected Vars
	 */
	protected $module;
	protected $file;
	
	protected function __construct($_params)
	{
		$this->module = $_params["module"];
		$this->file   = $_params["controller"];
		$this->params = $_params;
		
		$configIni   = new Nadeb_CrudConfig($this->module,$this->file);
		$gridInfos   = $configIni->get_gridInfo();
		$gridTable   = $configIni->get_tableConfig();
		$gridQuery   = $configIni->get_gridQuery();
		$gridColumns = $configIni->get_gridColumns();
		$gridTools   = $configIni->get_gridTools();
		
		$this->title    = $gridInfos["title"];
		$this->readOnly = $gridInfos["read_only"];
		$this->size     = $gridInfos['size'];
		
		$this->key      = $gridTable["key"];
		$this->table    = $gridTable['name'];
		
		$this->sql      = $gridQuery["query"];
		$this->columns  = $gridColumns;
		$this->tools    = $gridTools;
		
		if( isset($this->params['pg']) )
			$this->page = $this->params['pg'];
		else
			$this->page = 1;

		if( isset($this->params['rel']) )
			$this->sql = str_replace( '?', $this->params['rel'], $this->sql );
		else
			$this->sql = str_replace( '?', '-1', $this->sql );
		
	}
	
	protected function get_variables()
	{
		$this->headerConfigs["params"]     = $this->params;
		$this->headerConfigs["read_only"]  = $this->readOnly;
		$this->headerConfigs["columns"]    = $this->columns;
		$this->headerConfigs["tools"]      = $this->tools;
		
		$this->footerConfigs["params"]     = $this->params;
		$this->footerConfigs["page"]       = $this->page;
		$this->footerConfigs["totalPages"] = $this->totalPages;
		
		$this->bodyConfigs["params"]     = $this->params;
		$this->bodyConfigs["columns"]    = $this->columns;
		$this->bodyConfigs["data"]       = $this->data;
		$this->bodyConfigs["read_only"]  = $this->readOnly;
		$this->bodyConfigs["tools"]      = $this->tools;
		$this->bodyConfigs["key"]        = $this->key;
	}

	protected function get_data()
	{
		$db = Zend_Registry::get("db");
		$db->setFetchMode(Zend_Db::FETCH_ASSOC);
		
		$this->data = $db->fetchAll( $this->sql . $this->paginacao() );
	}

	protected function get_totalPages()
	{
		$db = Zend_Registry::get("db");
		$db->setFetchMode(Zend_Db::FETCH_ASSOC);
		$dados = $db->fetchAll($this->sql);
		
		$totReg = count($dados);
		
		return $this->totalPages = ceil( ($totReg / $this->size) );
	}
	
	protected function get_atualPage()
	{
		if(isset($this->page))
			$atualPage = $this->page;
		else
			$atualPage = 1;

		return $atualPage;
	}
	
	/*
	 * Adiciona no SQL um limite de registros que serão exibidos no objeto
	 */
	private function paginacao()
	{
		if($this->size)
		{
			if(!$this->page)
			{
				$page = "0";
			}
			else
			{
				$page = ($this->page * $this->size) - $this->size;
				if($this->page > $this->get_totalPages()) 
					$page = ($this->totalPages * $this->size) - $this->size;
			}

			return ' LIMIT ' . $page . ' , ' . $this->size;
		}
	}
	
	
}
